package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    //查询频道列表
    @Override
    public ResponseResult channelList() {
        //这里本身是不需要分页查询的,但是为了安全,防止数据过多,故使用分页查询,手动限制一下长度
        //最多显示100条
        Page<WmChannel> page = new Page<>(1, 100);
        Page<WmChannel> channelPage = wmChannelMapper.selectPage(page, null);
        //总数
        Integer total = Math.toIntExact(channelPage.getTotal());
        PageResponseResult result = new PageResponseResult(1, 100, total);
        //将数据赋值
        result.setData(channelPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult channelSave(WmChannel channel) {
        //入参判断
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "传入的channel参数为空");
        }
        //查重,拿前端传进来的频道名,去查询是否存在
        String channelName = channel.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(channelName != null, WmChannel::getName, channelName);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if (count > 0) {
            //大于0,说明重名了
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名重复了");
        }

        //走到这,说明没有重名,保存即可
        channel.setCreatedTime(new Date());
        channel.setIsDefault(true);
        boolean saveResult = this.save(channel);
        return saveResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }

    @Override
    public PageResponseResult listChannel(ChannelDto dto) {
        //判断空
        if (dto == null) {
            log.warn("传入的参数为空");
            return new PageResponseResult(0, 0, 0);
        }

        Integer pageNum = dto.getPage();
        Integer pageSize = dto.getSize();

        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        String name = dto.getName();
        //组装查询条件
        Page<WmChannel> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, WmChannel::getName, name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> selectPage = wmChannelMapper.selectPage(page, wrapper);

        Long total = selectPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(pageNum, pageSize, total.intValue());
        pageResponseResult.setData(selectPage.getRecords());
        return pageResponseResult;
    }

    /**
     * admin修改频道
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "参数为空");
        }

        Integer channelId = channel.getId();
        //判断当前频道是否包含新闻内容
        LambdaQueryWrapper<WmNews> newsWrapper = new LambdaQueryWrapper<>();
        newsWrapper.eq(channelId != null, WmNews::getChannelId, channelId);
        newsWrapper.eq(WmNews::getStatus, 9);
        Integer selectCount = wmNewsMapper.selectCount(newsWrapper);
        if (selectCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "当前频道包含新闻,无法修改");
        }
        boolean updateResult = this.updateById(channel);

        return updateResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult deleteChannel(Integer id) {
        if (id == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        LambdaQueryWrapper<WmChannel> channelWrapper = new LambdaQueryWrapper<>();
        channelWrapper.eq(WmChannel::getId, id);
        WmChannel channelFromDB = wmChannelMapper.selectOne(channelWrapper);
        if (channelFromDB == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        Boolean status = channelFromDB.getStatus();
        if (status) {
            //为true说明频道启动中,不能删除
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "启动中的频道不能删除");
        }
        Integer channelId = channelFromDB.getId();
        LambdaQueryWrapper<WmNews> newsWrapper = new LambdaQueryWrapper<>();
        newsWrapper.eq(channelId != null, WmNews::getChannelId, channelId);
        Integer newsCount = wmNewsMapper.selectCount(newsWrapper);
        if (newsCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道关联了新闻,无法删除");
        }
        boolean result = this.removeById(id);
        return result ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}